package org.samples.dao.imp;

import java.util.ArrayList;

import org.springframework.stereotype.Repository;
import org.samples.dao.IExamUserDAO;
import org.samples.model.PagerModel;
import org.samples.model.ExamUser;
import org.samples.support.SystemContext;

@Repository
public class ExamUserDAOImp extends BaseHibernateDAO<ExamUser> implements IExamUserDAO {
	
/*	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	private static Map<Integer, User> users = new HashMap<Integer, User>();*/
	
	public ExamUserDAOImp (){
	/*	users.put(1, new User("1", "alex", null, "13145121123", null, "广东理工职业学院", "2000-01-01", 0, null, 1));
		users.put(2, new User("2", "Jordan", null, "13145121123", null, "广东理工职业学院", "2000-01-01", 0, null, 1));
		users.put(3, new User("3", "james", null, "13145121123", null, "广东理工职业学院", "2000-01-01", 0, null, 1));
		users.put(4, new User("4", "Kobe", null, "13145121123", null, "广东理工职业学院", "2000-01-01", 0, null, 1));
		users.put(5, new User("5", "Ray", null, "13145121123", null, "广东理工职业学院", "2000-01-01", 0, null, 1));
		users.put(6, new User("6", "Linux", null, "13145121123", null, "广东理工职业学院", "2000-01-01", 0, null, 1));
		users.put(7, new User("7", "CentOS", null, "13145121123", null, "广东理工职业学院", "2000-01-01", 0, null, 1));*/
	}

	@SuppressWarnings("unchecked")
	@Override
	public PagerModel<ExamUser> findUserPage() {
		PagerModel<ExamUser> page = new PagerModel<ExamUser>();
		ArrayList<ExamUser> dates = new ArrayList<ExamUser>();
		int offset = SystemContext.getOffset();
		int pageSize = SystemContext.getSize();
		//使用hibernate完成分页
		dates = (ArrayList<ExamUser>) this.getSession().createQuery("From ExamUser").setFirstResult(offset).setMaxResults(pageSize).list();
		page.setDates(dates);
		page.setOffset(offset);
		page.setPageSize(pageSize);
		//获取总记录数
		long totalSize = (long) this.getSession().createQuery("select count(*) from ExamUser ").uniqueResult();
		page.setTotalSize(totalSize);
		return page;
		/*String sql = " SELECT * FROM exam_user; ";
		List<User> userList = jdbcTemplate.query(sql, new RowMapper<User>(){
			@Override
			public User mapRow(ResultSet rs, int arg1) throws SQLException {
				User user = new User();
				user.setAddress(rs.getString("address"));
				user.setBirthday(rs.getString("birthday"));
				user.setEmail(rs.getString("email"));
				user.setGender(rs.getInt("gender"));
				user.setLast_lock_time(rs.getString("last_lock_time"));
				user.setLogin_fail_count(rs.getInt("login_fail_count"));
				user.setPassword(rs.getString("password"));
				user.setTel(rs.getString("tel"));
				user.setUserId(rs.getString("user_id"));
				user.setUserName(rs.getString("user_name"));
				return user;
			}
		});
		return userList;*/
		/*if(users != null){
			return new ArrayList(users.values());
		} else{
			return null;
		}*/
	}

	@Override
	public ExamUser getById(String userId) {
		return this.get(userId);
		/*String sql = " SELECT * FROM exam_user WHERE user_id = ?; ";
		return jdbcTemplate.queryForObject(sql, new Object[]{userId}, new RowMapper<User>(){
			@Override
			public User mapRow(ResultSet rs, int arg1) throws SQLException {
				User user = new User();
				user.setAddress(rs.getString("address"));
				user.setBirthday(rs.getString("birthday"));
				user.setEmail(rs.getString("email"));
				user.setGender(rs.getInt("gender"));
				user.setLast_lock_time(rs.getString("last_lock_time"));
				user.setLogin_fail_count(rs.getInt("login_fail_count"));
				user.setPassword(rs.getString("password"));
				user.setTel(rs.getString("tel"));
				user.setUserId(rs.getString("user_id"));
				user.setUserName(rs.getString("user_name"));
				return user;
			}
		});*/
/*		if(users.containsKey(Integer.parseInt(userId))){
			return users.get(Integer.parseInt(userId));
		} else{
			return null;
		}*/
	}
	
	@Override
	public void editUser(ExamUser user) throws Exception {
			this.update(user);
		/*String sql = " UPDATE exam_user SET user_name = ?, password = ?, tel = ?, email = ?, address = ?, birthday = ?, gender = ?, last_lock_time = ?, login_fail_count = ? WHERE user_id = ?; ";
		jdbcTemplate.update(sql, new Object[]{user.getUserName(), user.getPassword(), user.getTel(), user.getEmail(), user.getAddress(), user.getBirthday(), user.getGender(), user.getLast_lock_time(), user.getLogin_fail_count(), user.getUserId()});*/
		/*if(user != null && users.containsKey(Integer.parseInt(user.getUserId()))){
			users.put(Integer.parseInt(user.getUserId()), user);
		} else{
			throw new Exception("Update User Fail!");
		}*/
	}

	@Override
	public void creatUser(ExamUser user) throws Exception {
		this.save(user);
		/*String sql = " INSERT INTO exam_user VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?); ";
		jdbcTemplate.update(sql, new Object[]{user.getUserId(), user.getUserName(), user.getPassword(), user.getTel(), user.getEmail(), user.getAddress(), user.getBirthday(), user.getGender(), user.getLast_lock_time(), user.getLogin_fail_count()});*/
		/*if(user != null && !users.containsKey(user.getUserId())){
			users.put(Integer.parseInt(user.getUserId()), user);
		} else{
			throw new Exception("Create User Fail!");
		}*/
	}

	@Override
	public void deleteUser(String userId) throws Exception {
		ExamUser existsUser = this.get(userId);
		if(existsUser != null) this.delete(existsUser);
		/*String sql = " DELETE FROM exam_user WHERE user_id = ?; ";
		jdbcTemplate.update(sql, new Object[]{userId});*/
		/*if(users.containsKey(Integer.parseInt(userId))){
			users.remove(Integer.parseInt(userId));
		} else{
			throw new Exception("Delete User Fail!");
		}*/
	}

}
